<template>
  <div class="login">
    <el-container>
      <!-- <el-header>
        <div class="">
          <img :src="csgImg" class="csgLogImag"/>
          <p>仓库消防模拟训练系统</p>
        </div>
      </el-header> -->
      <el-main>
        <div>
          <!-- <div class="titleCss">
          		<img src="../assets/image/title.png" alt="" />
          </div> -->
          <div class="login_bsk">
            <el-form ref="loginForm" :model="loginForm" :rules="loginRules" label-position="left" label-width="0px" class="login-form">
              <h3>
                <!-- <span class="title">{{ webName }}</span> -->
               <span class="titleLogin">系统登录</span>
              </h3>
              <el-form-item prop="username" style="margin-top: 30px;">
                <el-input v-model="loginForm.username" type="text" autocomplete="off" placeholder="请输入账号">
                  <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
                </el-input>
              </el-form-item>
              <el-form-item prop="password">
              <el-input v-model="loginForm.password" type="text" class="test"
                autocomplete="new-password" placeholder="请输入密码" @keyup.enter.native="handleLogin">
                  <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
                </el-input>
<!--              <el-input v-model="pwdCover" type="text" class="test"
                id="pwd"
                autocomplete="off"
                @input="setPassword"
                placeholder="请输入密码"
                @keyup.enter.native="handleLogin">
                  <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
                </el-input> -->
              </el-form-item>
              <!-- <el-form-item prop="code">
                <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%" @keyup.enter.native="handleLogin">
                  <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon"/>
                </el-input>
                <div class="login-code">
                  <img :src="codeUrl" @click="getCode">
                </div>
              </el-form-item> -->
              <!-- <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住我</el-checkbox> -->
              <el-form-item style="width:100%;">
                <el-button :loading="loading" size="medium" class="line-btn" style="width:100%;" @click.native.prevent="handleLogin">
                  <span v-if="!loading">登 录</span>
                  <span v-else>登 录 中...</span>
                </el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>

      </el-main>
      <!-- <el-footer>
        <div v-if="$store.state.settings.showFooter" id="el-login-footer">
          <span v-html="$store.state.settings.footerTxt"/>
          <a href="" target="_blank">{{ $store.state.settings.caseNumber }}</a>
        </div>
      </el-footer> -->
    </el-container>
  </div>
</template>

<script>
import { encrypt } from '@/utils/rsaEncrypt'
import Config from '@/config'
import { getCodeImg } from '@/api/framework/login/login'
export default {
  name: 'Login',
  data() {
    return {
      pwdCover:"",
      webName: Config.webName,
      codeUrl: '',
      cookiePass: '',
      clientId: Config.clientId,
      loginForm: {
        username: '',
        password: '',
        rememberMe: false,
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '用户名不能为空' }],
        password: [{ required: true, trigger: 'blur', message: '密码不能为空' }],
        // code: [{ required: true, trigger: 'change', message: '验证码不能为空' }]
      },
      loading: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    //this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.codeUrl = 'data:image/gif;base64,' + res.img
        this.loginForm.uuid = res.uuid
      })
    },
    getCookie() {
      const username = localStorage.getItem('username')
      let password = localStorage.getItem('password')
      const rememberMe = localStorage.getItem('rememberMe')
      // 保存cookie里面的加密后的密码
      this.cookiePass = password === null ? '' : password
      password = password === null ? this.loginForm.password : password
      this.loginForm = {
        username: username === null ? this.loginForm.username : username,
        password: password,
        rememberMe: rememberMe === null ? false : Boolean(rememberMe),
        code: ''
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        const user = {
          username: this.loginForm.username,
          password: this.loginForm.password,
          rememberMe: this.loginForm.rememberMe,
          code: this.loginForm.code,
          uuid: this.loginForm.uuid,
          clientId: this.clientId,
        }
        // if (user.password !== this.cookiePass) {
        //   user.password = encrypt(user.password)
        // }
        if (valid) {
          this.loading = true
          if (user.rememberMe) {
            localStorage.setItem('username', user.username)
            localStorage.setItem('password', user.password)
            localStorage.setItem('rememberMe', user.rememberMe)
          } else {
            localStorage.removeItem('username')
            localStorage.removeItem('password')
            localStorage.removeItem('rememberMe')
          }
          this.$store.dispatch('Login', user).then(() => {
            this.loading = false
            this.$router.push({ path: '/' })
            //去之前的页面
            //this.$router.push({ path: this.redirect || '/' })
          }).catch(() => {
            this.loading = false
            this.getCode()
          })
        } else {
          return false
        }
      })
    },
    setPassword(val) {
      if (this.isShowPassword) {
        this.loginForm.password = val;
      } else {
        let nDot = /[^·]/g; // 非圆点字符
        let index = -1; // 新输入的字符位置
        let lastChar = void 0; // 新输入的字符
        let realArr = this.loginForm.password.split(""); // 真实密码数组
        let coverArr = val.split(""); // 文本框显示密码数组
        let coverLen = val.length; // 文本框字符串长度
        let realLen = this.loginForm.password.length; // 真实密码长度
        // 找到新输入的字符及位置
        coverArr.forEach((el, idx) => {
          if (nDot.test(el)) {
            index = idx;
            lastChar = el;
          }
        });
        if (realLen < coverLen) {
          // 新增字符
          realArr.splice(index, 0, lastChar);
        } else if (coverLen <= realLen && index !== -1) {
          // 替换字符（选取一个或多个字符直接替换）
          realArr.splice(index, realLen - (coverLen - 1), lastChar);
        } else {
          // 删除字符，因为 val 全是 ● ，没有办法匹配，不知道是从末尾还是中间删除的字符，删除了几个，不好对 password 处理，所以可以通过光标的位置和 val 的长度来判断
          let pos = document.getElementById("pwd").selectionEnd; // 获取光标位置
          realArr.splice(pos, realLen - coverLen);
        }
        // 将 pwdCover 替换成 ●
        this.pwdCover = val.replace(/\S/g, "·");
        this.loginForm.password = realArr.join("");
      }
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" >

.test .el-input__inner{
  -webkit-text-security:disc;
}
  .el-input__inner:focus {
    outline: none;
    border-color: #dcdfe6;
  }
  .el-form-item.is-error .el-input__inner {
    border-color: #dcdfe6;
  }
  .titleCss{
  	height: 120px;
  	width: 700px;
  	position: relative;
  	margin: 0 auto;
    img{
      display: block;
          vertical-align: middle;
          max-width: 100%;
          height: auto;
    }
  }
  .el-form-item{
    margin-bottom: 35px;
  }

  .el-main {
    padding:0px;
    position: relative;
    align-content: center;
    background: url(../assets/logo/index.png) no-repeat 0px center;
    height: auto;
    width: 100%;
    min-width: 1400px;
    font-family: Microsoft YaHei,Arial;
    background-repeat: repeat-x;
    background-size: 100% auto;
    background-position: top;
  }
  .el-form-item__error{
    padding-top: 2px;
  }

 .csgLogImag {
    border-width:0px;
    position:absolute;
    left:0px;
    top: 10px;
    width: 180px;
    height: 56px;
  }
  .login {
    display: flex;
    justify-content: center;
    height: 100%;
    background-position: center;
    background-repeat: repeat-x;
  }
  .login_bsk {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 30px;
  }
  h3{
    height: 28px;
	  text-align: center;
	  line-height: 28px;
    font-weight: 800;
  }
  .titleLogin {
    margin-left: 10px;
    color: #fff;
    font-size: 21px;
    font-family:'微软雅黑 Bold', '微软雅黑 Regular', '微软雅黑';
  }
  .login-form {
    background: #ffffff;;
    opacity: 0.7;
    width: 380px;
    height: 380px;
    //box-shadow:2px 10px 20px rgba(0, 0, 0, 0.4);
    padding: 25px 25px 5px 25px;
    border-radius: 15px;
    .el-input {
      height: 40px;
      .el-input__inner {
        height: 40px;
        // background: #11151f;
        // color: #fff;
        //border-radius: 2px;
        border: none;
        background: #fff;
        opacity: 0.3;
        color: #000;
        //-webkit-text-security:disc;
      }
    }
    .input-icon{
      height: 40px;
      width: 14px;
      margin-left: 2px;
    }
  }
  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }
  .login-code {
    width: 33%;
    display: inline-block;
    height: 38px;
    float: right;
    img{
      margin-top: -2px;
      border-radius: 3px;
      cursor: pointer;
      vertical-align:middle
    }
  }

  .line-btn{
  	  width: 100%;
  	  color: #131925;
  	  background-color: #FFF;
  	  border-color: #FFF;
  	  border-radius: 2px;
      height: 40px;
  }
  .line-btn:hover,.line-btn:focus{
    color: #FFF;
    background-color: #dcdfe6;
    border-color: #dcdfe6;
  }
</style>
